<?php
namespace App\Library\Orders;
use Cloud;
use Exception;
use DB;
use App\Library\BTC\BitcoinFunction;
use App\Library\ETH\EthereumFunction;
use App\Library\OBA\ObaFunction;
use App\Models\Wallet;
use App\Models\Turn;

/**
 * Class Order
 * @package App\Library\Order1111
 */
class Turns
{
    /**
     * 转入生成钱包地址
     *
     * @ author: ZUORENCI
     * @ E-mail:904725327@qq.com
     * @ date: 2018/6/26
     * @access public|private|protected
     * @param  int    $uid    comment
     * @param  int    $cid    comment
     * @param  string    $c_name    comment
     * @return boolean|array        错误返回false，成功反会数组
     */
    public function into($uid,$cid,$c_name){
        if (empty($uid)||empty($cid)||empty($c_name))
            return false;
        $btc_model = new BitcoinFunction($c_name);
        $etc_model = new EthereumFunction();
        $oba_model = new ObaFunction();
        try {
            //开启事务
            $where1['status'] = 0;
            $where1['cid'] = $cid;
            DB::beginTransaction();
            $address = Wallet::where($where1)->first();
            //dump($address);die;
            if ($address){
                $address_now = $address->address;
                $address_id = $address->id;
                //查询冻结资产
                $money_b=0;
                switch ($c_name){
                    case 'BTC':
                    case  'LTC':
                    case  'DGC':
                        $money = $btc_model->getBalanceByAddress($address_now);
                        if ($money!==false){
                            $money_b = $money;
                        }
                        break;
                    case 'ETH':
                        $money = $etc_model->getBalanceByAddress($address_now);
                        if ($money!==false){
                            $money_b = $money;
                        }
                        break;
                    case 'OBA':
                        $money = $oba_model->getBalanceByAddress($address_now);
                        if ($money!==false){
                            $money_b = $money;
                        }
                        break;
                }
                if ($money_b){
                    $data['block_balance'] = $money_b;
                }
                $data['status'] = 1;
                $address->update($data);
            }else{
                switch ($c_name){
                    case 'BTC':
                    case  'LTC':
                    case  'DGC':
                        $new_address = $btc_model->createWalletAddress();
                        break;
                    case 'ETH':
                        $new_address = $etc_model->createWalletAddress();
                        break;
                }
                if ($new_address===false){
                    return FALSE;
                }else{
                    $address_now = $new_address;
                }
                $data['cid'] = $cid;
                $data['address'] = $address_now;
                $data['status'] = 1;
                $address = Wallet::create($data);
                $address_id = $address->id;
            }
            $data1['uid'] = $uid;
            $data1['cid'] = $cid;
            $data1['wallet_address'] = $address_now;
            $data1['type'] = 1;
            $data1['status'] = 1;
            $data1['wallet_id'] = $address_id;
            $data1['trade_time'] = time()+60*60*2;
            $data1['unlock_time'] = $data1['trade_time']+60*60*6;
            $data1['order_no'] = get_microtimes();
            Turn::create($data1);
            DB::commit();
            return $address->address;
        } catch (Exception $e) {
            DB::rollback();
            return false;
        }
    }
}
